function G = speciter1d(N)
% Solve the Green's function of -u'' + (1+sin(x)) u  on [0,1] with
% periodic boundary condition with spectral method and CG iterative
% scheme.
N = 32;
h = 1 / N;
xmesh = (0:N-1)'*h;
V = 1+sin(2*pi*xmesh);
f = eye(N);
nrhs = N;
maxiter = 500;
u0 = f;
r = f - specA1d(u0,V);
w = -r;
z = specA1d(w,V);
a = zeros(1,nrhs);
a = sum(r.*w,1) ./ sum(w.*z,1);
a = spdiags(a',0,nrhs,nrhs);
ux = u0 + w*a;
B = 0;

for i = 1 : maxiter
  r = r - z * a;
  if( norm(r,'fro') / nrhs < 1e-10 )
    break;
  end
  B = sum(r.*z,1)./sum(w.*z,1);
  B = spdiags(B',0,nrhs,nrhs);
  w = -r + w*B;
  z = specA1d(w,V);
  a = sum(r.*w,1) ./ sum(w.*z,1);
  a = spdiags(a',0,nrhs,nrhs);
  ux = ux + w*a;
end
G = ux;
